<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace Yj\Watcher\Command;


use Yj\Watcher\Option;
use Yj\Watcher\Options;
use Yj\Watcher\Watcher;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputOption;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Yj\Watcher\YjWatcher;

#[Command]
class WatchCommand extends HyperfCommand
{
    protected $container;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'yj:watch';

    public function __construct(ContainerInterface $container)
    {
        parent::__construct(); //命令

        $this->container = $container; //容器类
        //描述
        $this->setDescription('watch command');

        //参数
        $this->addOption('file', 'F', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', []);
        //参数
        $this->addOption('dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', []);
        //no-restart
        $this->addOption('no-restart', 'N', InputOption::VALUE_NONE, 'Whether no need to restart server');
    }


    public function handle()
    {
        $option = make(Option::class, [
            'dir' => $this->input->getOption('dir'),
            'file' => $this->input->getOption('file'),
            'restart' => ! $this->input->getOption('no-restart'),
        ]);

        $watcher = make(YjWatcher::class, [
            'option' => $option,
            'output' => $this->output,
        ]);

        $watcher->run();
    }
}
